package com.cssy.yd.basic.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.entity.BasicCompanyDO;
import com.cssy.yd.basic.entity.BasicCompanytypeDO;
import com.cssy.yd.basic.service.IBasicCompanyService;
import com.cssy.yd.basic.service.IBasicCompanyTypeService;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.AjaxUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Controller
@RequestMapping("/yd/basic/company")
public class CompanyController extends BaseController<BasicCompanyDO> {
	private static final String MANAGER = "/yd/basic/company/CompanyManager";// 列表页面
	private static final String CUSTOMERSMANAGER = "/yd/basic/customers/CustomersManager";// 列表页面
	private static final String SUPPLIERMANAGER = "/yd/basic/supplier/SupplierManager";// 列表页面
	private static final String EIDT = "/yd/basic/company/CompanyOperation";// 添加、修改、显示页面
	private static final String CUSTOMERSEIDT = "/yd/basic/customers/CustomersOperation";// 添加、修改、显示页面
	private static final String SUPPLIEREIDT = "/yd/basic/supplier/SupplierOperation";// 添加、修改、显示页面

	@Resource(name = "basicCompanyService")
	private IBasicCompanyService basicCompanyService;

	@Resource(name = "basicCompanyTypeService")
	private IBasicCompanyTypeService basicCompanyTypeService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;

	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		String type = request.getParameter("type");
		model.addAttribute("type", type);
		 SearchConditions scs = new SearchConditions();
		 SearchResult result= basicCompanyTypeService.findListByExample(scs);
		 List<BasicCompanytypeDO> bd=result.getResults();
		 model.addAttribute("companytype", bd);
		return MANAGER;
	}
	
	@RequestMapping("/customersManager")
	public String customersManager(HttpServletRequest request, Model model)
			throws Exception {
		String type = request.getParameter("type");
		model.addAttribute("type", type);
		 SearchConditions scs = new SearchConditions();
		 SearchResult result= basicCompanyTypeService.findListByExample(scs);
		 List<BasicCompanytypeDO> bd=result.getResults();
		 model.addAttribute("companytype", bd);
		return CUSTOMERSMANAGER;
	}

	@RequestMapping("/supplierManager")
	public String supplierManager(HttpServletRequest request, Model model)
			throws Exception {
		String type = request.getParameter("type");
		model.addAttribute("type", type);
		 SearchConditions scs = new SearchConditions();
		 SearchResult result= basicCompanyTypeService.findListByExample(scs);
		 List<BasicCompanytypeDO> bd=result.getResults();
		 model.addAttribute("companytype", bd);
		return SUPPLIERMANAGER;
	}
	
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String parentname = request.getParameter("parentname");
		 JqGridHandler hander=new JqGridHandler(request);
		 SearchConditions scs = new SearchConditions();
		 hander.getWheres(scs);
		 if((parentname!=null||"".equals(parentname))&&"1".equals(parentname)){
			 scs.addCondition("parentname", "客户", SearchCondition.OP_EQUAL);
		 }else if((parentname!=null||"".equals(parentname))&&"2".equals(parentname)){
			 scs.addCondition("parentname", "供应商", SearchCondition.OP_EQUAL);
		 }else{
			 scs.addCondition("parentname", "MES供应商", SearchCondition.OP_EQUAL);
		 }
		 if(scs.getOrderBy()==null||"".equals(scs.getOrderBy())){
			 scs.setOrderBy("code desc");
		 }
		 SearchResult result= basicCompanyService.findListByExample(scs);
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);

	}

	@Override
	public String operationForm(HttpServletRequest request,
			@PathVariable String id, @PathVariable String flag, Model model)
			throws Exception {
		String parent = request.getParameter("parent");
		 SearchConditions scs = new SearchConditions();
		 if((parent!=null||"".equals(parent))&&"1".equals(parent)){
			 scs.addCondition("name", "客户", SearchCondition.OP_EQUAL);
		 }else if((parent!=null||"".equals(parent))&&"2".equals(parent)){
			 scs.addCondition("name", "供应商", SearchCondition.OP_EQUAL);
		 }else{
			 scs.addCondition("name", "MES供应商", SearchCondition.OP_EQUAL);
		 }
		 SearchResult result= basicCompanyTypeService.findListByExample(scs);
		 List<BasicCompanytypeDO> bd=result.getResults();
		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			model.addAttribute("companytype", bd);
		} else {
			BasicCompanyDO bcd =basicCompanyService.findListById(id);
			model.addAttribute("company", bcd);
			model.addAttribute("companytype", bd);
			model.addAttribute("flag", "EDIT");
		}
		if(parent!=null&&"1".equals(parent)){
			return CUSTOMERSEIDT;
		}else if(parent!=null&&"2".equals(parent)){
			return SUPPLIEREIDT;
		}else{
			return EIDT;
		}
	}
	
	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicCompanyDO curAP, BindingResult result, Model model) {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改
				basicCompanyService.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(curAP.getId());
			} else {// 新增
				curAP.setName(curAP.getName().trim());
				curAP.setCode(curAP.getCode().trim());
				curAP.setBankDeposit(curAP.getBankDeposit().trim());
				curAP.setAccountNum(curAP.getAccountNum().trim());
				curAP.setAccountName(curAP.getAccountName().trim());
				curAP.setAttachperson(curAP.getAttachperson().trim());
				curAP.setNature(curAP.getNature().trim());
				curAP.setEmail(curAP.getEmail().trim());
				curAP.setTelephone(curAP.getTelephone().trim());
				curAP.setMobil(curAP.getMobil().trim());
				curAP.setAddress(curAP.getAddress().trim());
				curAP.setFax(curAP.getFax().trim());
				curAP.setSimplejakecn(curAP.getSimplejakecn().trim());
				curAP.setRemark(curAP.getRemark().trim());
				BasicCompanyDO cdo = null;
				if(curAP.getCode()==null||"".equals(curAP.getCode())){
					curAP.setCode(syCodeService.getCode("CM","CM",SyCode.DATE_YYMM,5));
				}else{
					 SearchConditions scs = new SearchConditions();
					 scs.addCondition("code",curAP.getCode(),SearchCondition.OP_EQUAL);
					 SearchResult sr= basicCompanyService.findListByExample(scs);
					 if(sr.getResults().size()>0){
						bodyMap.isFail();
						bodyMap.configWebInfor("对不起您输入的编码已存在");
						return bodyMap.modelMap;
					 }
				}
				cdo = basicCompanyService.save(curAP,SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(cdo.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;

	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				BasicCompanyDO bcdo = basicCompanyService.findListById(field);
				if(bcdo.getId()!=null){
					bcdo.setRemoved(1L);
					basicCompanyService.update(bcdo, SessionUtil.getUser(request));
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}

}
